public class NumIslands {

    public int numIslands(char[][] grid) {
        int ans = 0;
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') ans++;
                process(grid, i, j, m, n);
            }
        }
        return ans;
    }

    public void process(char[][] grid, int i, int j, int m, int n) {
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') return;
        grid[i][j] = '0';
        process(grid, i + 1, j, m, n);
        process(grid, i - 1, j, m, n);
        process(grid, i, j + 1, m, n);
        process(grid, i, j - 1, m, n);
    }
}
